//
//  MRContentOtherViewController.m
//  MRDemo
//
//  Created by dingping on 2020/8/18.
//  Copyright © 2020 dingping. All rights reserved.
//

#import "MRContentOtherViewController.h"
#import "MRNestTableViewCell.h"
static NSString *cellId = @"MRNestTableViewCell";
@interface MRContentOtherViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *contentTableView;

@end

@implementation MRContentOtherViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view addSubview:self.contentTableView];
}


#pragma mark - 加载数据
- (void)loadData
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.contentTableView.mj_header endRefreshing];
    });
}


#pragma mark - 初始化视图
- (UITableView *)contentTableView {
    if (!_contentTableView) {
        _contentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-44) style:UITableViewStyleGrouped];
        _contentTableView.delegate = self;
        _contentTableView.dataSource = self;

        _contentTableView.backgroundColor = [UIColor clearColor];
        [_contentTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        _contentTableView.showsVerticalScrollIndicator = NO;
        
        _contentTableView.contentInset = UIEdgeInsetsMake(0, 0, 90, 0);
        
        [_contentTableView registerClass:[MRNestTableViewCell class] forCellReuseIdentifier:cellId];

        _contentTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 0.001)];
        _contentTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 0.001)];

        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
        _contentTableView.mj_header = header;
        
        
        if (@available(iOS 11.0, *)) {
            _contentTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _contentTableView.estimatedRowHeight = 0;
        }
        else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _contentTableView;
}


#pragma mark - UITableViewDelegate & UITableViewDataSource -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MRNestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

#pragma mark - scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(self.delegate && [self.delegate respondsToSelector:@selector(subScrollViewDidScroll:)]) {
        [self.delegate subScrollViewDidScroll:scrollView];
    }
}


@end
